package com.orcamento.entity.report;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.orcamento.entity.Event;
import com.orcamento.entity.enums.Month;
import com.orcamento.entity.report.subreport.EventGain;
import com.orcamento.entity.report.subreport.EventSpent;
import com.orcamento.util.Money;

public class EventReport extends Report {

	private List<EventGain> moreGains = new ArrayList<EventGain>();
	private List<EventSpent> moreSpents = new ArrayList<EventSpent>();
	private List<EventSpent> moreCreditCardsSpents = new ArrayList<EventSpent>();
	private List<EventSpent> moreCashSpents = new ArrayList<EventSpent>();
	
	public EventReport(Month month, Integer year) {
		super(month, year);
	}

	@Override
	public void generate() {
		generateEventsWithMoreGainsReport();
		generateEventsWithMoreSpentsReport();
		generateEventsWithMoreCashSpentsReport();
		generateEventsWithMoreCreditCardSpentsReport();
	}

	private void generateEventsWithMoreCreditCardSpentsReport() {
		Money totalSpentAllEvents = new Money();
		
		for( Event event : events ) {
			Money totalCreditCardSpentEvent = event.getTotalCreditCardSpents(month, year);
			totalSpentAllEvents = totalSpentAllEvents.plus( totalCreditCardSpentEvent );
			
			moreCreditCardsSpents.add( new EventSpent(event.getName(), totalCreditCardSpentEvent) );
		}
		
		for( EventSpent eventSpent : moreCreditCardsSpents ) {
			eventSpent.setPercentage(totalSpentAllEvents);
		}
		
		Collections.sort( moreCreditCardsSpents, Collections.reverseOrder() );
	}

	private void generateEventsWithMoreCashSpentsReport() {
		Money totalCashSpentAllEvents = new Money();
		
		for( Event event : events ) {
			Money totalSpentEvent = event.getTotalCashSpents(month, year);
			totalCashSpentAllEvents = totalCashSpentAllEvents.plus( totalSpentEvent );
			
			moreCashSpents.add( new EventSpent(event.getName(), totalSpentEvent) );
		}
		
		for( EventSpent eventSpent : moreCashSpents ) {
			eventSpent.setPercentage(totalCashSpentAllEvents);
		}
		
		Collections.sort( moreCashSpents, Collections.reverseOrder() );
	}

	private void generateEventsWithMoreSpentsReport() {
		Money totalSpentAllEvents = new Money();
		
		for( Event event : events ) {
			Money totalSpentEvent = event.getTotalSpents(month, year);
			totalSpentAllEvents = totalSpentAllEvents.plus( totalSpentEvent );
			
			moreSpents.add( new EventSpent(event.getName(), totalSpentEvent) );
		}
		
		for( EventSpent eventSpent : moreSpents ) {
			eventSpent.setPercentage(totalSpentAllEvents);
		}
		
		Collections.sort( moreSpents, Collections.reverseOrder() );
	}

	private void generateEventsWithMoreGainsReport() {
		Money totalGainAllEvents = new Money();
		
		for( Event event : events ) {
			Money totalGainEvent = event.getTotalGain(month, year);
			totalGainAllEvents = totalGainAllEvents.plus( totalGainEvent );
			
			moreGains.add( new EventGain(event.getName(), totalGainEvent) );
		}
		
		for( EventGain eventGain : moreGains ) {
			eventGain.setPercentage(totalGainAllEvents);
		}
		
		Collections.sort( moreGains, Collections.reverseOrder() );
	}

	public List<EventGain> getMoreGains() {
		return moreGains;
	}

	public List<EventSpent> getMoreSpents() {
		return moreSpents;
	}

	public List<EventSpent> getMoreCreditCardsSpents() {
		return moreCreditCardsSpents;
	}

	public List<EventSpent> getMoreCashSpents() {
		return moreCashSpents;
	}
	
}
